package com.example.app2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.app2.sort.sortA;
import com.example.app2.sort.sortB;
import com.example.app2.viewpager.ListAd;
import com.example.app2.weather.ListW;
import com.example.app2.weather.weather;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {
    public com.example.app2.weather.weather weather;
    private TextView tt;
    public static List<ListW> list = new ArrayList<>();
    private ListAd listAd;
    private Boolean istrue = true;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);


        tt = findViewById(R.id.tt);
        imageView = findViewById(R.id.up);
        imageView.setOnClickListener(this);
//        mhandler.postDelayed(mrun,3000);
        getList();
    }

    private Handler mhandler = new Handler(){};

    private Runnable mrun = new Runnable() {

        @Override
        public void run() {
            getList();
            mhandler.postDelayed(this,3000);
        }
    };
    private void getList() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request build = new Request.Builder().url("https://mock.w2code.com/api/weather").build();
        okHttpClient.newCall(build).enqueue(new Callback() {



            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                weather = new Gson().fromJson(string, weather.class);
                runOnUiThread(new Runnable() {



                    @Override
                    public void run() {
                        try {
                            JSONArray arr = new JSONObject(string).getJSONObject("data").getJSONArray("list");
                            list = new ArrayList<>();
                            for (int i = 0; i < arr.length(); i++) {
                                JSONObject jsonObject = arr.getJSONObject(i);
                                String maximum = jsonObject.getString("maximum");
                                String minimum = jsonObject.getString("minimum");
                                String status = jsonObject.getString("status");
                                ListW listW = new ListW(maximum,minimum,status);
                                list.add(listW);

                            }
                            ListView listView = findViewById(R.id.listview);
                            listAd = new ListAd(MainActivity3.this, list);
                            listView.setAdapter(listAd);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.up:
                Toast.makeText(MainActivity3.this,"79789",Toast.LENGTH_SHORT).show();
                if(istrue){
                    Collections.sort(list,new sortA());
                    imageView.setImageResource(R.drawable.sun);
                    istrue = false;
                }else {
                    Collections.sort(list,new sortB());
                    imageView.setImageResource(R.drawable.air);
                    istrue = true;
                }


                listAd.notifyDataSetChanged();
                break;
        }
    }
}